module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('files', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      tenant_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'tenants',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: '租户ID',
      },
      user_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '上传用户ID',
      },
      original_name: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '原始文件名',
      },
      file_name: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '存储文件名',
      },
      mime_type: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '文件类型',
      },
      size: {
        type: Sequelize.BIGINT,
        allowNull: false,
        comment: '文件大小（字节）',
      },
      path: {
        type: Sequelize.STRING(500),
        allowNull: false,
        comment: '文件存储路径',
      },
      url: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '文件访问URL',
      },
      category: {
        type: Sequelize.ENUM('document', 'image', 'video', 'audio', 'other'),
        allowNull: false,
        defaultValue: 'other',
        comment: '文件分类',
      },
      bucket: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '存储桶名称（OSS/S3）',
      },
      storage_type: {
        type: Sequelize.ENUM('local', 'oss', 's3'),
        allowNull: false,
        defaultValue: 'local',
        comment: '存储类型',
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '文件描述',
      },
      metadata: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '文件元数据',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
      },
    });

    // 添加索引
    await queryInterface.addIndex('files', ['tenant_id']);
    await queryInterface.addIndex('files', ['user_id']);
    await queryInterface.addIndex('files', ['category']);
    await queryInterface.addIndex('files', ['storage_type']);
    await queryInterface.addIndex('files', ['created_at']);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('files');
  },
};

